package com.itheima.mall.dubbo.api.sms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mall.commons.utils.StringUtils;
import com.itheima.mall.dubbo.mapper.sms.HomeNewProductMapper;
import com.itheima.mall.entity.domain.sms.HomeBrand;
import com.itheima.mall.entity.domain.sms.HomeNewProduct;
import com.itheima.mall.entity.domain.vo.smsre.HomeBrandResult;
import com.itheima.mall.entity.domain.vo.smsre.ResultAll;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class HomeNewProductServiceImpl extends ServiceImpl<HomeNewProductMapper, HomeNewProduct> implements HomeNewProductService {
    @Autowired
    private HomeNewProductMapper homeNewProductMapper;

    @Override
    public void updateSort(Long id, Integer sort) {
        HomeNewProduct homeNewProduct = new HomeNewProduct();
        homeNewProduct.setId(id);
        homeNewProduct.setSort(sort);
        homeNewProductMapper.updateById(homeNewProduct);
    }

    @Override
    public ResultAll page(Integer pageNum, Integer pageSize, Integer recommendStatus, String productName) {
        Page<HomeNewProduct> homeNewProductPage = new Page<>(pageNum, pageSize);
        ResultAll resultAll = new ResultAll();
        LambdaQueryWrapper<HomeNewProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(productName), HomeNewProduct::getProductName, productName);
        queryWrapper.eq(recommendStatus != null, HomeNewProduct::getRecommendStatus, recommendStatus);
        homeNewProductMapper.selectPage(homeNewProductPage, queryWrapper);
        resultAll.setList(homeNewProductPage.getRecords());
        resultAll.setPageNum(pageNum);
        resultAll.setPageSize(pageSize);
        resultAll.setTotal(homeNewProductPage.getTotal());
        resultAll.setTotalPage(homeNewProductPage.getPages());

        return resultAll;
    }
}
